/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.farsunset.lvxin.mvc.controller.api;

import com.farsunset.cim.sdk.server.model.Message;
import com.farsunset.lvxin.cim.push.DefaultMessagePusher;
import com.farsunset.lvxin.dto.BaseResult;
import com.farsunset.lvxin.dto.MessageResult;
import com.farsunset.lvxin.service.MessageService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/message")
@Api(produces = "application/json", consumes = "application/json", tags = "消息接口")
public class APIMessageController {


    @Resource
    private MessageService messageService;

    @ApiOperation(httpMethod = "POST", value = "发送消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sender", value = "发送者", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "receiver", value = "接受者", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "action", value = "消息类型", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "content", value = "消息内容", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "format", value = "内容格式", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "extra", value = "扩展", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "title", value = "标题", paramType = "query", dataType = "String"),
    })

    @PostMapping(value = "/send")
    public MessageResult send(@RequestParam String sender,
                              @RequestParam String receiver,
                              @RequestParam String action,
                              @RequestParam(required = false) String content,
                              @RequestParam(required = false) String format,
                              @RequestParam(required = false) String extra,
                              @RequestParam(required = false) String title) {

        Message message = new Message();

        message.setSender(sender);
        message.setReceiver(receiver);
        message.setAction(action);
        message.setContent(content);
        message.setFormat(format);
        message.setExtra(extra);
        message.setTitle(title);


        defaultMessagePusher.push(message);

        MessageResult result = new MessageResult();

        result.id = message.getId();
        result.timestamp = message.getTimestamp();

        return result;
    }

    @Resource
    private DefaultMessagePusher defaultMessagePusher;

    @ApiOperation(httpMethod = "GET", value = "阅读消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "消息ID", required = true, paramType = "path", dataType = "Long"),
    })

    @GetMapping(value = "/read/{id}")
    public BaseResult read(@PathVariable Long id) {

        messageService.updateState(id, Message.STATE_READ);

        return BaseResult.make();
    }


    @ApiOperation(httpMethod = "GET", value = "接收单个消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "消息ID", required = true, paramType = "path", dataType = "Long"),
    })
    @GetMapping(value = "/received/{id}")
    public BaseResult received(@PathVariable Long id) {
        messageService.updateState(id, Message.STATE_RECEIVED);
        return BaseResult.make();
    }

    @ApiOperation(httpMethod = "GET", value = "接收全部消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "用户账号", required = true, paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/received/batch")
    public BaseResult batchReceive(@RequestParam String account) {

        messageService.updateBatchReceived(account);

        return BaseResult.make();
    }

    @ApiOperation(httpMethod = "GET", value = "删除消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "消息ID", required = true, paramType = "path", dataType = "Long"),
    })
    @DeleteMapping(value = "/{id}")
    public BaseResult delete(@PathVariable Long id) {

        messageService.delete(id);

        return BaseResult.make();
    }


    @ApiOperation(httpMethod = "GET", value = "获取离线消息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "用户账号", required = true, paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/list/offline")
    public BaseResult offlineList(@RequestParam String account) {

        return BaseResult.makeData(messageService.queryOffLineMessages(account));
    }


}
